home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.text;
-
- import java.awt.Rectangle;
- import java.awt.Shape;
-
- public abstract class CompositeView extends View {
- private static View[] ONE = new View[1];
- private static View[] ZERO = new View[0];
- private View[] children = new View[1];
- private int nchildren = 0;
- private short left;
- private short right;
- private short top;
- private short bottom;
-
- public CompositeView(Element elem) {
- super(elem);
- }
-
- public void append(View v) {
- ONE[0] = v;
- this.replace(this.nchildren, 0, ONE);
- }
-
- protected abstract void childAllocation(int var1, Rectangle var2);
-
- protected final short getBottomInset() {
- return this.bottom;
- }
-
- public Shape getChildAllocation(int index, Shape a) {
- Rectangle alloc = a.getBounds();
- this.childAllocation(index, alloc);
- return alloc;
- }
-
- protected Rectangle getInsideAllocation(Shape a) {
- if (a != null) {
- Rectangle alloc = new Rectangle(a.getBounds());
- alloc.x += this.left;
- alloc.y += this.top;
- alloc.width -= this.left + this.right;
- alloc.height -= this.top + this.bottom;
- return alloc;
- } else {
- return null;
- }
- }
-
- protected final short getLeftInset() {
- return this.left;
- }
-
- protected final short getRightInset() {
- return this.right;
- }
-
- protected final short getTopInset() {
- return this.top;
- }
-
- public View getView(int n) {
- return this.children[n];
- }
-
- protected abstract View getViewAtPoint(int var1, int var2, Rectangle var3);
-
- protected View getViewAtPosition(int pos, Rectangle a) {
- Element elem = ((View)this).getElement();
- int index = elem.getElementIndex(pos);
- Element child = elem.getElement(index);
- if (child != null && index < this.getViewCount()) {
- View v = this.getView(index);
- if (v.getElement() == child) {
- if (a != null) {
- this.childAllocation(index, a);
- }
-
- return v;
- }
- }
-
- return null;
- }
-
- public int getViewCount() {
- return this.nchildren;
- }
-
- public void insert(int offs, View v) {
- ONE[0] = v;
- this.replace(offs, 0, ONE);
- }
-
- protected abstract boolean isAfter(int var1, int var2, Rectangle var3);
-
- protected abstract boolean isBefore(int var1, int var2, Rectangle var3);
-
- protected void loadChildren(ViewFactory f) {
- Element e = ((View)this).getElement();
- int n = e.getElementCount();
- if (n > 0) {
- View[] added = new View[n];
-
- for(int i = 0; i < n; ++i) {
- added[i] = f.create(e.getElement(i));
- }
-
- this.replace(0, 0, added);
- }
-
- }
-
- public Shape modelToView(int pos, Shape a) throws BadLocationException {
- Rectangle alloc = this.getInsideAllocation(a);
- View v = this.getViewAtPosition(pos, alloc);
- if (v != null) {
- int p0 = v.getStartOffset();
- int p1 = v.getEndOffset();
- if (pos >= p0 && pos < p1) {
- return v.modelToView(pos, alloc);
- }
- }
-
- throw new BadLocationException("Position not represented by view", pos);
- }
-
- public void removeAll() {
- this.replace(0, this.nchildren, ZERO);
- }
-
- public void replace(int offset, int length, View[] views) {
- for(int i = offset; i < offset + length; ++i) {
- this.children[i].setParent((View)null);
- }
-
- int delta = views.length - length;
- int src = offset + length;
- int nmove = this.nchildren - src;
- int dest = src + delta;
- if (this.nchildren + delta >= this.children.length) {
- int newLength = Math.max(2 * this.children.length, this.nchildren + delta);
- View[] newChildren = new View[newLength];
- System.arraycopy(this.children, 0, newChildren, 0, offset);
- System.arraycopy(views, 0, newChildren, offset, views.length);
- System.arraycopy(this.children, src, newChildren, dest, nmove);
- this.children = newChildren;
- } else {
- System.arraycopy(this.children, src, this.children, dest, nmove);
- System.arraycopy(views, 0, this.children, offset, views.length);
- }
-
- this.nchildren += delta;
-
- for(int i = 0; i < views.length; ++i) {
- views[i].setParent(this);
- }
-
- }
-
- protected final void setInsets(short top, short left, short bottom, short right) {
- this.top = top;
- this.left = left;
- this.right = right;
- this.bottom = bottom;
- }
-
- protected final void setParagraphInsets(AttributeSet attr) {
- this.top = (short)((int)StyleConstants.getSpaceAbove(attr));
- this.left = (short)((int)StyleConstants.getLeftIndent(attr));
- this.bottom = (short)((int)StyleConstants.getSpaceBelow(attr));
- this.right = (short)((int)StyleConstants.getRightIndent(attr));
- }
-
- public void setParent(View parent) {
- super.setParent(parent);
- if (parent != null) {
- ViewFactory f = ((View)this).getViewFactory();
- this.loadChildren(f);
- }
-
- }
-
- public int viewToModel(float x, float y, Shape a) {
- Rectangle alloc = this.getInsideAllocation(a);
- if (this.isBefore((int)x, (int)y, alloc)) {
- return ((View)this).getStartOffset();
- } else if (this.isAfter((int)x, (int)y, alloc)) {
- return ((View)this).getEndOffset() - 1;
- } else {
- View v = this.getViewAtPoint((int)x, (int)y, alloc);
- return v != null ? v.viewToModel(x, y, alloc) : -1;
- }
- }
- }
-